package com.huarui.notice.dao;

import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.ewaf.framework.dao.impl.BaseDaoImpl;
import org.ewaf.framework.model.PagingSummary;

import com.huarui.notice.Notice;

public class NoticeDaoImpl extends BaseDaoImpl implements INoticeDao {
	private Logger logger = Logger.getLogger(NoticeDaoImpl.class);
	/*
	 * private String namespace="notice";
	 * 
	 * 
	 * public void setNamespace(String namespace) { this.namespace = namespace;
	 * }
	 */

	@Override
	public void create(Notice notice) {

		super.create(notice);

		// getSqlMapClientTemplate().insert(namespace+ ".create", notice);
	}

	@Override
	public Notice queryById(int noticeId) {
		return (Notice) getSqlMapClientTemplate().queryForObject(
				namespace + ".queryById", Integer.valueOf(noticeId));
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Notice> queryByReceiverId(int receiveType, int receiverId) {
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("receiveType", receiveType);
		params.put("receiverId", Integer.valueOf(receiverId));
		return (List<Notice>) getSqlMapClientTemplate().queryForList(
				namespace + ".queryByReceiverId", params);
	}

	@Override
	public boolean update(Notice notice) {

		return getSqlMapClientTemplate().update(namespace + ".update", notice) == 1;
	}
	
	public boolean updateDealed(int receiverId,Integer[] ids,boolean dealed) {

		if (ids == null || ids.length == 0) {
			return true;
		}
		String idStr=StringUtils.join(ids,",");
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("receiverId", receiverId);
		params.put("ids", idStr);
		params.put("dealed", dealed);
		return getSqlMapClientTemplate().update(namespace + ".updateDealed", params) == ids.length;
	}
	

	public boolean updateReaden(int receiverId, Integer[] ids, boolean readen)
	{
		if (ids == null || ids.length == 0) {
			return true;
		}
		String idStr=StringUtils.join(ids,",");
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("receiverId", receiverId);
		params.put("ids", idStr);
		params.put("readen", readen);
		return getSqlMapClientTemplate().update(namespace + ".updateReaden", params) == ids.length;
	}
	
	public boolean delete(int receiverId,Integer[] ids) {
		if (ids == null || ids.length == 0) {
			return true;
		}
		String idStr=StringUtils.join(ids,",");
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("receiverId", receiverId);
		params.put("ids", idStr);
		return getSqlMapClientTemplate().delete(
				namespace + ".deleteNoticesById", params) == ids.length;

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Notice> queryBySenderId(int senderId) {
		// TODO Auto-generated method stub
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("senderId", Integer.valueOf(senderId));
		return (List<Notice>) getSqlMapClientTemplate().queryForList(
				namespace + ".queryBySenderId", params);
	}

	@SuppressWarnings("unchecked")
	public List<Notice> queryPagingNotices(Notice condition,
			PagingSummary summary) {
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("receiverId", condition.getReceiverId());
		params.put("dealed", condition.isDealed() ? "1" : "0");
		params.put("start", summary.getStart());
		params.put("limit", summary.getLimit());
		params.put("id", condition.getId());
		//logger.info("分页查询的参数==="+params);
		return (List<Notice>) getSqlMapClientTemplate().queryForList(
				namespace + ".queryPagingNotices", params);
	}

	public int qqueryNoticeCount(Notice condition) {
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("receiverId", condition.getReceiverId());
		params.put("dealed", condition.isDealed() ? "1" : "0");
		params.put("id", condition.getId());
		return (Integer) getSqlMapClientTemplate().queryForObject(
				namespace + ".queryNoticeCount", params);

	}
}
